<?php
namespace User\Logic;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class SmsLogic
{

    function sendSms($tel, $TemplateCode,$TemplateParam,$type)
    {
        AlibabaCloud::accessKeyClient('LTAIzDvdjn502tTM', '2YzJf8AK8hoRuZErZ3BVErjsvPVxLj')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $tel,
                        'TemplateCode' => $TemplateCode,//"SMS_209820984",
                        'TemplateParam' => $TemplateParam,//"{name:'$name',integral:" . $integral  . "}",
                        "SignName" => "天佑康宁",
                    ],
                ])
                ->request();
            $str = implode(', ',$result->toArray());
            M('sms_log')->add(
                array(
                    'type'=>$type,
                    'mobile'=>$tel,
                    'content'=>$str,
                    'create_at'=>date('Y-m-d H:i:s'),
                    'tplcode' => $TemplateCode
                )
            );
            \Think\Log::write($str);
            //print_r($result->toArray());
        } catch (ClientException $e) {
            \Think\Log::write($e->getErrorMessage() . PHP_EOL);
        } catch (ServerException $e) {
            \Think\Log::write($e->getErrorMessage() . PHP_EOL);
        }
    }
}